package com.loveprogrammer.eternity.skill.support.skills.passive;

import com.loveprogrammer.eternity.enums.QualityEnum;
import com.loveprogrammer.eternity.factory.MonsterFactory;
import com.loveprogrammer.eternity.factory.template.MonsterTemplate;
import com.loveprogrammer.eternity.model.Character;
import com.loveprogrammer.eternity.model.Monster;
import com.loveprogrammer.eternity.common.Tuple;
import com.loveprogrammer.eternity.utils.RandomBalance;

import java.util.*;

/**
 * @version 1.0.0
 * @description: 野性召唤
 * @author: eric
 * @date: 2022-12-22 11:31
 **/
public class SkillWildCall extends AbstractPassiveCallSkill {

    private Map<Integer,Integer> callWolvesPercents;
    private Map<Integer,Integer> callBearPercents;

    private List<Integer> qualitys = Arrays.asList(10000,4000,800,300,200);

    public SkillWildCall(int id, int baseInterval, int level, int order, int maxLevel, String name, String desc) {
        super(id, baseInterval, level, order, maxLevel, name, desc);
        callWolvesPercents = new LinkedHashMap<>();
        callWolvesPercents.put(90001,30000);
        callWolvesPercents.put(90002,9900);
        callWolvesPercents.put(90003,1900);
        callWolvesPercents.put(401,590);
        callWolvesPercents.put(480,190);
        callWolvesPercents.put(90004,90);

        callBearPercents = new LinkedHashMap<>();
        callBearPercents.put(90005,10000);
        callBearPercents.put(90006,100);

    }

    @Override
    public boolean checkLevelUp(int characterLevel) {
        // 每2级升一级
        if (characterLevel % 2 == 0) {
            return true;
        }
        return false;
    }

    @Override
    protected Tuple<String, List<Character>> skillLaunch(Character character,
                                                         List<? extends Character> attack,
                                                         List<? extends Character> defences) {
        Tuple<String, List<Character>> tuple = new Tuple<>();

        StringBuilder builder = new StringBuilder();
        List<Character> characters = new ArrayList<>(attack);
        builder.append("技能").append(getName()).append("(").append(getLevel()).append("级)释放");
        int increment = getHarm(character);
        // 召唤 1-10级 狼 10-20级 狼+熊
        if (getLevel() < 30) {
            callWolves(character, builder, characters, increment);
        } else if (getLevel() < 60) {
            // 狼 + 熊 熊的等级是（技能等级-10）/2 + 1
            callWolves(character, builder, characters, increment);
            callBeer(character, builder, characters, increment);
        } else {
            // 狼 + 熊
        }
        // 收到普通攻击，减少伤害
        tuple.setValue(characters);
        tuple.setKey(builder.toString());
        return tuple;
    }

    @Override
    protected int computeHarm(Character character) {
        return getLevel() * 10;
    }

    @Override
    protected int computeSecondHarm(Character character) {
        return 0;
    }

    private void callBeer(Character character, StringBuilder builder, List<Character> characters, int increment) {
        // 熊的等级是技能等级/2 + 1 数量是（技能等级/5 + 1） 一只熊 技能等级越高 召唤出高质量的熊的机率越大
        int num = 1;
        int level = character.getLevel() + getLevel() / 3 + 1;
        // 先找寻是哪种熊
        Map<Integer,Integer> percents = new LinkedHashMap<>();
        callBearPercents.forEach(
                (k,v) -> {
                    if(v > 10000) {
                        percents.put(k, Math.max(v - getLevel() * 100,10000));
                    } else if(v > 3000) {
                        percents.put(k, Math.max(v - getLevel() * 50,3000));
                    } else if(v > 1000) {
                        percents.put(k, Math.max(v - getLevel() * 30,1000));
                    } else {
                        percents.put(k, Math.min(v + v * getLevel()/5,1000));
                    }
                }
        );
        for (int i = 0; i < num; i++) {
            Integer id = RandomBalance.findByPercents(percents);
            MonsterTemplate template = MonsterFactory.getById(id);
            QualityEnum quality = RandomBalance.findQuality(qualitys.get(0) + increment,
                    qualitys.get(1) + increment, qualitys.get(2) + increment,
                    qualitys.get(3) + increment, qualitys.get(4) + increment
                    , 0, 0);
            Monster monster = MonsterFactory.createMonster(template, level, quality, true);
            builder.append(monster.toString()).append("加入战团").append("\n");
            characters.add(monster);
        }
    }

    private void callWolves(Character character, StringBuilder builder, List<Character> characters, int increment) {
        // 狼的等级是技能等级/2 + 1 数量是（技能等级/7 + 1） 最大三条狼 技能等级越高 召唤出高质量的狼的机率越大
        int num = Math.min(getLevel() / 7 + 1,3);
        int level = character.getLevel() + getLevel() / 3 + 1;
        // 先找寻是哪种狼 白狼无技能 血狼（血爪）疾星狼（血爪、迅捷） 极地雪狼 极地狼王（光环）星皇狼（吞噬、光环）
        Map<Integer,Integer> percents = new LinkedHashMap<>();
        callWolvesPercents.forEach(
                (k,v) -> {
                    // 如果等级大于10 去掉值大于10000的  人物20级以上就不会召唤出白狼了
                    if(getLevel() > 10 && v > 10000){
                        return;
                    }
                    // 人物35级以上就不会召唤出血狼了
                    if(getLevel() > 18 && v > 5000){
                        return;
                    }
                    // 人物50级以上就不会召唤出疾星狼了
                    if(getLevel() > 25 && v > 1000){
                        return;
                    }
                    // 人物70级以上就不会召唤出极地雪狼了
                    if(getLevel() > 35 && v > 200){
                        return;
                    }
                    percents.put(k,v);
                }
        );
        for (int i = 0; i < num; i++) {
            Integer id = RandomBalance.findByPercents(percents);
            MonsterTemplate template = MonsterFactory.getById(id);
            QualityEnum quality = RandomBalance.findQuality(qualitys.get(0) + increment,
                    qualitys.get(1) + increment, qualitys.get(2) + increment,
                    qualitys.get(3) + increment, qualitys.get(4) + increment
                    , 0, 0);
            Monster monster = MonsterFactory.createMonster(template, level, quality, true);
            builder.append(monster.toString()).append("加入战团").append("\n");
            characters.add(monster);
        }
    }
}
